package cn.jinjimi.app.store.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.custom.ui.fragment.CustomerClueListFragment;
import cn.jinjimi.app.store.DaoSession;
import cn.jinjimi.app.store.entity.ClueEntity;

import java.util.List;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.QueryBuilder;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.

/**
 * DAO for table "CLUE_ENTITY".
 */
public class ClueEntityDao extends AbstractDao<ClueEntity, String> {

    public static final String TABLENAME = "CLUE_ENTITY";

    /**
     * Properties of entity ClueEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property ClueId = new Property(0, String.class, "clueId", true, "CLUE_ID");
        public final static Property CustomerId = new Property(1, String.class, "customerId", false, "CUSTOMER_ID");
        public final static Property Phone = new Property(2, String.class, "phone", false, "PHONE");
        public final static Property RealName = new Property(3, String.class, "realName", false, "REAL_NAME");
        public final static Property Event = new Property(4, String.class, "event", false, "EVENT");
        public final static Property Content = new Property(5, String.class, "content", false, "CONTENT");
        public final static Property LinkUrl = new Property(6, String.class, "linkUrl", false, "LINK_URL");
        public final static Property Source = new Property(7, String.class, "source", false, "SOURCE");
        public final static Property CreatedTime = new Property(8, Long.class, "createdTime", false, "CREATED_TIME");
        public final static Property Advice = new Property(9, String.class, "advice", false, "ADVICE");
        public final static Property IsNew = new Property(10, Boolean.class, "isNew", false, "IS_NEW");
        public final static Property SsoId = new Property(11, String.class, "ssoId", false, "SSO_ID");
        public final static Property Category = new Property(12, String.class, "category", false, "CATEGORY");
        public final static Property SourceCode = new Property(13, String.class, "sourceCode", false, "SOURCE_CODE");
    }

    ;


    public ClueEntityDao(DaoConfig config) {
        super(config);
    }

    public ClueEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\"CLUE_ENTITY\" (" + //
                "\"CLUE_ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: clueId
                "\"CUSTOMER_ID\" TEXT," + // 1: customerId
                "\"PHONE\" TEXT," + // 2: phone
                "\"REAL_NAME\" TEXT," + // 3: realName
                "\"EVENT\" TEXT," + // 4: event
                "\"CONTENT\" TEXT," + // 5: content
                "\"LINK_URL\" TEXT," + // 6: linkUrl
                "\"SOURCE\" TEXT," + // 7: source
                "\"CREATED_TIME\" INTEGER," + // 8: createdTime
                "\"ADVICE\" TEXT," + // 9: advice
                "\"IS_NEW\" INTEGER," + // 10: isNew
                "\"SSO_ID\" TEXT," +  // 11: ssoId
                "\"CATEGORY\" TEXT," + // 12:category
                "\"SOURCE_CODE\" TEXT);");// 13:sourceCode

    }

    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, ClueEntity entity) {

        stmt.clearBindings();

        String clueId = entity.clueId;
        if (clueId != null) {
            stmt.bindString(1, clueId);
        }

        String customerId = entity.customerId;
        if (customerId != null) {
            stmt.bindString(2, customerId);
        }

        String phone = entity.phone;
        if (phone != null) {
            stmt.bindString(3, phone);
        }

        String realName = entity.realName;
        if (realName != null) {
            stmt.bindString(4, realName);
        }

        String event = entity.event;
        if (event != null) {
            stmt.bindString(5, event);
        }

        String content = entity.content;
        if (content != null) {
            stmt.bindString(6, content);
        }

        String linkUrl = entity.linkUrl;
        if (linkUrl != null) {
            stmt.bindString(7, linkUrl);
        }

        String source = entity.source;
        if (source != null) {
            stmt.bindString(8, source);
        }

        Long createdTime = entity.createdTime;
        if (createdTime != null) {
            stmt.bindLong(9, createdTime);
        }

        String advice = entity.advice;
        if (advice != null) {
            stmt.bindString(10, advice);
        }

        Boolean isNew = entity.isNew;
        if (isNew != null) {
            stmt.bindLong(11, isNew ? 1L : 0L);
        }

        String ssoId = entity.ssoId;
        if (ssoId != null) {
            stmt.bindString(12, ssoId);
        }

        String category = entity.category;
        if (category != null) {
            stmt.bindString(13, category);
        }

        String sourceCode = entity.sourceCode;
        if (sourceCode != null) {
            stmt.bindString(14, sourceCode);
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }

    /**
     * @inheritdoc
     */
    @Override
    public ClueEntity readEntity(Cursor cursor, int offset) {
        ClueEntity entity = new ClueEntity( //
                cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // clueId
                cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // customerId
                cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // phone
                cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // realName
                cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // event
                cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // content
                cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // linkUrl
                cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // source
                cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8), // createdTime
                cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // advice
                cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0, // isNew
                cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // ssoId
                cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // category
                cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13)
        );
        return entity;
    }

    /**
     * @inheritdoc
     */
    @Override
    public void readEntity(Cursor cursor, ClueEntity entity, int offset) {
        entity.clueId = (cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.customerId = (cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.phone = (cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.realName = (cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.event = (cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.content = (cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.linkUrl = (cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.source = (cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.createdTime = (cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8));
        entity.advice = (cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.isNew = (cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0);
        entity.ssoId = (cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.category = (cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.sourceCode = (cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
    }

    /**
     * @inheritdoc
     */
    @Override
    protected String updateKeyAfterInsert(ClueEntity entity, long rowId) {
        return entity.clueId;
    }

    /**
     * @inheritdoc
     */
    @Override
    public String getKey(ClueEntity entity) {
        if (entity != null) {
            return entity.clueId;
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

    /**
     * 根据服务器返回Clues插入数据
     *
     * @param clues
     */
    public List<ClueEntity> insertOrUpdate(List<ClueEntity> clues) {
        for (int i = 0; i < clues.size(); i++) {
            clues.set(i, insertOrUpdate(clues.get(i)));
        }
        return clues;
    }

    public ClueEntity insertOrUpdate(final ClueEntity clue) {

        ClueEntity clueEntity = queryBuilder().where(Properties.ClueId.eq(clue.clueId))
                .unique();
        if (clueEntity != null) {
            clue.isNew = clueEntity.isNew;
        }
        if (clue.isNew && System.currentTimeMillis() - clue.createdTime > Constants.DAY_15) {
            clue.isNew = false;
        }

        clue.ssoId = AccountManager.getInstance().getAccount().ssoid;

        insertOrReplace(clue);
        return clue;
    }

    public List<ClueEntity> queryClueEntities(String source, int limit) {
        QueryBuilder<ClueEntity> querybuilder = queryBuilder()
                .where(Properties.SsoId.eq(AccountManager.getInstance().getAccount().ssoid))
                .orderDesc(Properties.CreatedTime)
                .limit(limit);
        if (StringUtils.isNotEmpty(source)) {
            if (source.equals(CustomerClueListFragment.SourceDrp))
                querybuilder.where(Properties.SourceCode.eq(source));
            else {
                querybuilder.where(Properties.SourceCode.notEq(CustomerClueListFragment.SourceDrp));
            }
        }

        List<ClueEntity> clueEntities = querybuilder.list();
        for (ClueEntity clueEntity : clueEntities) {
            if (clueEntity.isNew && System.currentTimeMillis() - clueEntity.createdTime > Constants.DAY_15) {
                clueEntity.isNew = false;
                insertOrReplace(clueEntity);
            }
        }
        return clueEntities;
    }

    /**
     * Drops the underlying database table.
     */
    public static void dropTable(SQLiteDatabase db, boolean exit) {
        String sql = "DROP TABLE " + (exit ? "IF EXISTS " : "") + "\"CLUE_ENTITY\"";
        db.execSQL(sql);
    }

}
